//
//  RoadViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "RoadViewController.h"
#import "LSAppTools.h"
#import "RoadModel.h"
#import "RoadTableViewCell.h"
#import "RoadDetialViewController.h"
@interface RoadViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *myArray;

@end

@implementation RoadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = [NSString stringWithFormat:@"%@行程",self.roadModel.name];
    UIImage *leftImage = [UIImage imageNamed:@"iconfont-fanhui-2.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) style:UITableViewStylePlain];
    
    
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[RoadTableViewCell class] forCellReuseIdentifier:@"RoadTableViewCell"];
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myArray = [NSMutableArray array];

    [self getAFNetWorkingData];

}

- (void)getAFNetWorkingData{
    NSLog(@"%@",self.roadModel.nameId);
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/plans/%@.json", self.roadModel.nameId];
    [LSAppTools getDataWithURL:url blcok:^(id result) {
        NSMutableArray *bigArray = [NSMutableArray arrayWithArray:result];
        for (NSMutableDictionary *dic in bigArray) {
            RoadModel *roadModel = [[RoadModel alloc] init];
            [roadModel setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:roadModel];
            
        }
        [self.myTableView reloadData];

        
    }];
    
}

- (void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"RoadTableViewCell";
    
    RoadTableViewCell *roadCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    // 取消点击置灰效果
    roadCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    RoadModel *roadModel = [self.myArray objectAtIndex:indexPath.row];
    
    roadCell.roadModel = roadModel;
    
    return roadCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 225;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    RoadDetialViewController *roadDetailVC = [[RoadDetialViewController alloc] init];
    RoadModel *roadModel = [self.myArray objectAtIndex:indexPath.row];
    roadDetailVC.roadModel = roadModel;
    
    
    
    
    [self.navigationController pushViewController:roadDetailVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
